﻿namespace Pottery.Module.Store
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Pottery.Module.Infrastructure;
    using Pottery.Module.Infrastructure.Interfaces;
    using Pottery.Module.Store.Models;
    using Pottery.Module.Infrastructure.Models;

    /// <summary>
    /// Repository for Store
    /// </summary>
    public class StoreStorage : IRepository
    {
        #region Constructors

        private StoreStorage()
        {
            m_BaseRepository = new XmlRepository();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets instance of LoadStorage
        /// </summary>
        public static StoreStorage Instance
        {
            get
            {
                lock (__mutex)
                {
                    if (m_SalesXmlRepository == null)
                        m_SalesXmlRepository = new StoreStorage();

                    return m_SalesXmlRepository;
                }
            }
        }

        #endregion

        #region IRepository Implementation

        public string GetStorageName<T>() where T : class
        {
            return m_BaseRepository.GetStorageName<T>();
        }

        public bool IsInitialized<T>() where T : class
        {
            return m_BaseRepository.IsInitialized<T>();
        }

        public void Initialize<T>(string connectionString) where T : class
        {
            m_BaseRepository.Initialize<T>(connectionString);
        }

        public void Add<T>(T tableEntity) where T : class
        {
            m_BaseRepository.Add<T>(tableEntity);
        }

        public void Update<T>(T tableEntity) where T : class
        {
            m_BaseRepository.Update<T>(tableEntity);
        }

        public void Delete<T>(T tableEntity) where T : class
        {
            m_BaseRepository.Delete<T>(tableEntity);
        }

        public T Create<T>() where T : class
        {
            ///TODO:CREATE
            if (typeof(T) == typeof(UserHistory))
                return new UserHistory() as T;

            if (typeof(T) == typeof(User))
                return new User() as T;

            throw new NotSupportedException("Entity type is not supported");
        }

        public T Item<T, Key>(Key key) where T : class
        {
            return m_BaseRepository.Item<T, Key>(key);
        }

        public IQueryable<T> GetQuery<T>() where T : class
        {
            return m_BaseRepository.GetQuery<T>();
        }

        public IEnumerable<T> Enumerate<T>() where T : class
        {
            return m_BaseRepository.Enumerate<T>();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool managedDispose)
        {
            if (m_Dispose)
                return;

            if (managedDispose)
            {
                ///TODO:maneger resources
            }

            m_BaseRepository.Dispose();

            m_Dispose = true;
        }

        #endregion

        #region Finalize

        ~StoreStorage()
        {
            Dispose(false);
        }
        #endregion

        #region Private Fields

        private static StoreStorage m_SalesXmlRepository = default(StoreStorage);
        private readonly static object __mutex = new object();
        private static XmlRepository m_BaseRepository;
        private bool m_Dispose = false;

        #endregion
    }
}
